﻿using System;
using System.Linq;

using Lilium.CSharp.Expressions;
using Lilium.CSharp.Expressions.Substitutions;

namespace ExpressionBuilderSample
{
    public class MyFilter<TEntity, TField>
    {
        public string FieldName;
        public TField FieldValue;

        public IQueryable<TEntity> Apply(IQueryable<TEntity> source)
        {
            var predicate = Transformers.Transform<Func<TEntity, bool>>(
                entity => entity.PropertyOrField<TField>(FieldName).Equal(FieldValue));

            return source.Where(predicate);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            //IsNullOrEmptySubstitution();

            using (var context = new DatabaseEntities())
            {
                var filter = new MyFilter<Employee, string>()
                {
                    FieldName = "LastName",
                    FieldValue = "Smith"
                };

                var john = filter.Apply(context.Employee).FirstOrDefault();

                var predicate2 = Transformers.Transform<Func<Employee, bool>>(
                    emp => emp.PropertyOrField<string>("LastName") == emp.PropertyOrField<string>("FirstName"));

                var dog = context.Employee.FirstOrDefault(predicate2);
            
            }
        }

        /// <remarks>
        /// Useless for EF but can be used with LINQ2SQL
        /// </remarks>
        static void IsNullOrEmptySubstitution()
        {
            using (var context = new DatabaseEntities())
            {   //Func<Employee, bool> prediace = (Employee entity) => string.IsNullOrEmpty(entity.FirstName);

                var test = context.Employee.Where(
                    
                    employee => string.IsNullOrEmpty(employee.FirstName)
                    
                    ).SingleOrDefault();
            }
        }
    }
}
